<?php
    /*

     Version: MPL 1.1

     Software distributed under the License is distributed on an "AS IS"
     basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
     License for the specific language governing rights and limitations
     under the License.

     The Original Code is KASSIOP Framework.

     The Initial Developer of the Original Code is SURIAN Nicolas (aka NairuS) <me@nairus.fr>.
     Portions created by the Initial Developer are Copyright (C) 2010
     The Initial Developer. All Rights Reserved.

     Contributor(s):

     Alternatively, the contents of this file may be used under the terms
     of the MPL license Version 1.1 (the  "MPL" License"), in which case the
     provisions of Version: MPL 1.1 License are applicable instead of those
     above.  If you wish to allow use of your version of this file only
     under the terms of the MPL License and not to allow others to use
     your version of this file under the MPL, indicate your decision by
     deleting  the provisions above and replace  them with the notice and
     other provisions required by the MPL License.  If you do not delete
     the provisions above, a recipient may use your version of this file
     under either the MPL License.

     The contents of this file are subject to the Mozilla Public License
     Version 1.1 (the "License"); you may not use this file except in
     compliance with the License. You may obtain a copy of the License at
     http://www.mozilla.org/MPL/

    */


    require_once 'PHPUnit/Framework.php';
    require_once dirname( __FILE__ ) . '/../../../../kassiop/core/Autoload.php' ;
    Autoload::register() ;

    /**
     * The unit test class of the StringValidator class.
     *
     * @package    unittests.kassiop.system
     * @subpackage validators
     *
     * @author NairuS
     * @version 1.3 $Rev: 90 $ $Date: 2011-01-16 09:23:26 +0000 (Sun, 16 Jan 2011) $ $Author: nicolas.surian $
     */
    class StringValidatorTest extends PHPUnit_Framework_TestCase
    {
        /**
         * Defines the validator to test.
         *
         * @var StringValidator
         */
        protected $validator ;

        protected function setUp()
        {
            $this->validator = new StringValidator( array( StringValidator::$trimKey => true ) ) ;
        }

        protected function tearDown()
        {
            unset( $this->validator ) ;
        }

        public function testClean()
        {
            $this->assertEquals( $this->validator->clean(' Test ' ), 'Test', 'The string must be trimmed.' ) ;

            $this->tearDown() ;
            $this->validator = new StringValidator( array( StringValidator::$trimKey => true, StringValidator::$minLengthKey => 6, StringValidator::$maxLengthKey => 10 ) );
            $this->assertEquals( $this->validator->clean(' Test 6 ' ), 'Test 6', 'The string must be cleaned and valid.' ) ;

            try
            {
                $this->validator->clean( ' test ' );
            }
            catch( Exception $e )
            {
                $this->assertTrue( $e instanceof ErrorBaseValidator, 'The validator result must be an instance of ErrorBaseValidator.' ) ;
                $this->assertEquals( $e->getMessage(), '"test" is too short (6 characters min).', 'The error message must be well formed.' ) ;
            }

            try
            {
                $this->validator->clean( ' test123456789 ' );
            }
            catch( Exception $e )
            {
                $this->assertTrue( $e instanceof ErrorBaseValidator, 'The validator result must be an instance of ErrorBaseValidator.' ) ;
                $this->assertEquals( $e->getMessage(), '"test123456789" is too long (10 characters max).', 'The error message must be well formed.' ) ;
            }

            $this->setUp() ;
        }

        public function testGetOption()
        {
            $this->assertEquals( $this->validator->getOption( StringValidator::$emptyValueKey), null, '1- The "' . StringValidator::$emptyValueKey . '" option has to be null.' ) ;
            $this->assertEquals( $this->validator->getOption( StringValidator::$requiredKey), true, '2- The "' . StringValidator::$requiredKey . '" option has to be boolean "true".' ) ;
            $this->assertEquals( $this->validator->getOption( StringValidator::$trimKey), true, '3- The "' . StringValidator::$trimKey . '" option has to be boolean "true".' ) ;

            $this->tearDown() ;
            $this->validator = new StringValidator( array( StringValidator::$minLengthKey => 6, StringValidator::$maxLengthKey => 50, StringValidator::$trimKey => false ) );
            $this->assertEquals( $this->validator->getOption( StringValidator::$trimKey), false, '4- The "' . StringValidator::$trimKey . '" option has to be boolean "false".' ) ;
            $this->assertEquals( $this->validator->getOption( StringValidator::$maxLengthKey), 50, '5- The "' . StringValidator::$maxLengthKey . '" option has to be equals to "50".' ) ;
            $this->assertEquals( $this->validator->getOption( StringValidator::$minLengthKey), 6, '6- The "' . StringValidator::$minLengthKey . '" option has to be equals to "6".' ) ;
            $this->setUp() ;
        }

        public function testGetMessage()
        {
            $this->assertEquals( $this->validator->getMessage( StringValidator::$invalidKey), 'Invalid.', 'The "' . StringValidator::$invalidKey . '" message has not to be null.' ) ;
            $this->assertEquals( $this->validator->getMessage( StringValidator::$requiredKey), 'Required.', 'The "' . StringValidator::$requiredKey . '" message has not to be null.' ) ;
            $this->assertEquals( $this->validator->getMessage( StringValidator::$maxLengthKey), '"%value%" is too long (%max_length% characters max).', 'The "' . StringValidator::$maxLengthKey . '" message has not to be null.' ) ;
            $this->assertEquals( $this->validator->getMessage( StringValidator::$minLengthKey), '"%value%" is too short (%min_length% characters min).', 'The "' . StringValidator::$minLengthKey . '" message has not to be null.' ) ;
        }

        public function testSetOption()
        {
            $this->assertEquals( $this->validator->getOption( StringValidator::$requiredKey ), true, '1- The "' . StringValidator::$requiredKey . '" option must be setted to true.' ) ;

            $this->validator->setOption( StringValidator::$requiredKey, false) ;
            $this->assertEquals( $this->validator->getOption( StringValidator::$requiredKey ), false, '2- The "' . StringValidator::$requiredKey . '" option must be setted to false.' ) ;

            try
            {
                $this->validator->setOption( 'not_exists', "test" );
            }
            catch( Exception $e )
            {
                $this->assertTrue( $e instanceof InvalidArgumentException, "The setOption method has to throw an exception: " . $e->getMessage() );
            }
        }
    }
?>